﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Modes;
using Zor_Tools;
namespace Zor_DAL
{
    public class Zor_CertificateRecord_DAL<T> where T : class
    {
        /// <summary>
        /// 完税证作废
        /// </summary>
        /// <param name="id">房屋Id集合</param>
        /// <returns></returns>
        public static BaseAnswer CertificateCancel(int id, int AcceptanceId, int PtId, int CRID)
        {
            int usId = RequestSession.GetSessionUser().User_Id;
            int datime = TimeHelper.ToNowTimeStamp();
            List<string> lstsql = new List<string>();
            lstsql.Add(string.Format("update [Zor_CertificateRecord] set CR_Date = " + datime + ",CR_UserId = " + usId + ",CR_State = 2 where CR_Id = " + id + ""));
            lstsql.Add(string.Format("update [Zor_PayTaxes] set Pt_CertificateNumber = null,Pt_Date = null,pt_UserId=null,Pt_State=0 where Pt_Id = " + PtId + ""));
            lstsql.Add(string.Format("update [Zor_Acceptance] set Acceptance_State = 6 where Acceptance_Id = " + AcceptanceId + ""));
            lstsql.Add(string.Format("update [Zor_Certificate] set Certificate_Invalid = Certificate_Invalid+1 where Certificate_Id = " + CRID + ""));
            lstsql.Add(string.Format("if  exists(select Pt_Id from Zor_Invoice where  Pt_Id= " + PtId + ")delete from Zor_Invoice where Pt_Id= " + PtId + " "));
            //错误sql语句
            //lstsql.Add(string.Format("if  exists(select Sm_Id from Zor_Summary where In_Id in( select In_Id from Zor_Summary where  Pt_Id= " + PtId + ")) delete from Zor_Summary where Sm_Id in (  select Sm_Id from Zor_Summary where In_Id in( select In_Id from Zor_Summary where  Pt_Id= " + PtId + ") )"));
            //修改后sql
            lstsql.Add(string.Format("if  exists(select Sm_Id from Zor_Summary where In_Id in (select In_Id from Zor_Invoice where Pt_Id in (select pt_id from dbo.Zor_PayTaxes where  Pt_Id= " + PtId + "))) delete from Zor_Summary where Sm_Id in ( select Sm_Id from Zor_Summary where In_Id in (select In_Id from Zor_Invoice where Pt_Id in (select pt_id from dbo.Zor_PayTaxes where  Pt_Id= " + PtId + "))  )"));
            
            //如果销号了，不能再完税证作废(sql语句错误)
            //lstsql.Add(string.Format("if  exists( select Dn_Id from Zor_Summary where In_Id in( select In_Id from Zor_Invoice where  Pt_Id= " + PtId + "))delete from Zor_Disannul where Dn_Id in (select Dn_Id from Zor_Summary where In_Id in( select In_Id from Zor_Invoice where  Pt_Id= " + PtId + ")) and Dn_State=1"));           
            BaseAnswer bar = DapperTransaction.DoTransaction(lstsql);
            return bar;
        }
    }
}
